package com.tfj.mvp.tfj.home.arround;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.tfj.DemoApplication;
import com.hyphenate.tfj.R;
import com.jakewharton.rxbinding2.view.RxView;
import com.tfj.comm.bean.Constant;
import com.tfj.mvp.base.BaseActivity;
import com.tfj.mvp.tfj.detail.huxing.VHuXingActivity;
import com.tfj.mvp.tfj.home.adapter.HouseDataAdapter;
import com.tfj.mvp.tfj.home.bean.HouseDataBean;
import com.tfj.mvp.tfj.live.bean.TagBean;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Liang_Lu on 2017/12/21.
 *
 * @author HuangQiang
 * @github https://github.com/HuangQiang
 */

public class VArroundActivity extends BaseActivity<PArroundImpl> implements CArround.IVArround , AMapLocationListener {


    @BindView(R.id.textView_location)
    TextView textViewLocation;
    @BindView(R.id.recyclew_tag)
    RecyclerView recyclewTag;
    @BindView(R.id.recyclew_content)
    RecyclerView recyclewContent;
    public AMapLocationClient mLocationClient = null;
    @Override
    protected void initView() {
        super.initView();
        setTitleText("附近房源");
        //定位
        addDisposable(RxView.clicks(textViewLocation).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                .subscribe(o -> {

                }));
        //标签
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclewTag.setLayoutManager(linearLayoutManager);
        recyclewTag.setAdapter(quickAdapter_tag);
        List<TagBean> tagBeans = new ArrayList<>();
        tagBeans.add(new TagBean("新房"));
        tagBeans.add(new TagBean("门店"));
        quickAdapter_tag.replaceData(tagBeans);
        //房源
        List<HouseDataBean> houseDataBeans = new ArrayList<>();
        houseDataBeans.add(new HouseDataBean());
        houseDataBeans.add(new HouseDataBean());
        houseDataBeans.add(new HouseDataBean());
        houseDataBeans.add(new HouseDataBean());
        HouseDataAdapter houseDataAdapter = new HouseDataAdapter(this);
        LinearLayoutManager linearLayoutManager_arround = new LinearLayoutManager(this);
        recyclewContent.setLayoutManager(linearLayoutManager_arround);
        recyclewContent.setAdapter(houseDataAdapter);
        houseDataAdapter.replaceData(houseDataBeans);
        initLocation();
    }

    @Override
    public int setContentViewId() {
        return R.layout.activity_arround;
    }

    @Override
    public void createPresenter() {
        mPresenter = new PArroundImpl(mContext, this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation.getErrorCode() == 0) {
            Log.i(TAG, "城市信息--->" + amapLocation.getCity() + "城区信息--->" + amapLocation.getDistrict());
            textViewLocation.setText(amapLocation.getCity());
            showToast("定位成功");
        } else {
            textViewLocation.setText("定位失败");
        }
    }
    private void initLocation() {
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置为单次定位
        option.setOnceLocation(true);
        /**
         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
         */
        mLocationClient = new AMapLocationClient(DemoApplication.getInstance());
//设置定位回调监听
        mLocationClient.setLocationListener(this);
        if (null != mLocationClient) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                //没有权限，向系统申请该权限。
                Log.i(TAG, "没有权限");
                //这里添加向系统请求权限操作
            } else {
                //已经获得权限，则执行定位请求。
                mLocationClient.setLocationOption(option);
                //mLocationClient，再调用start以保证场景模式生效
                mLocationClient.stopLocation();
                mLocationClient.startLocation();
                Log.i(TAG, "开始定位");
            }

        }
    }
    private int checkPosition = 0;
    private QuickAdapter_Tag quickAdapter_tag = new QuickAdapter_Tag();
    public class QuickAdapter_Tag extends BaseQuickAdapter<TagBean, BaseViewHolder> {
        public QuickAdapter_Tag() {
            super(R.layout.item_tag);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final TagBean item) {
            RelativeLayout rl_out = viewHolder.getView(R.id.rl_out);
            TextView textView_name = viewHolder.getView(R.id.textView_name);
            textView_name.setText(item.getName());
            textView_name.setTextColor(viewHolder.getPosition() == checkPosition ? Color.parseColor("#ffffff") : Color.parseColor("#ffbfc2cc"));
            rl_out.setBackgroundResource(viewHolder.getPosition() == checkPosition ? R.drawable.shape_background_main_2 : R.drawable.shape_background_gray_2);
            addDisposable(RxView.clicks(rl_out).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                    .subscribe(o -> {
                        checkPosition = viewHolder.getPosition();
                        quickAdapter_tag.notifyDataSetChanged();
                    }));
        }
    }

}
